package com.joe.networkdisk.controller;

import com.joe.networkdisk.service.PageProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ckh
 * @since 2020/11/25 4:45 PM
 */
@Controller
@CrossOrigin
public class PageController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private HttpServletRequest httpServletRequest;

    @Resource
    private PageProvider pageProvider;

    /**
     * 首页跳转
     */
    @GetMapping("/")
    public String index() {
        logger.info("首页跳转请求URL：{}", httpServletRequest.getRequestURL());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        logger.info("首页跳转数据处理开始");
        String resultUrl = pageProvider.indexHandle();
        logger.info("首页跳转数据处理结束,resultUrl:{}", resultUrl);
        stopWatch.stop();
        logger.info("首页跳转调用时间,millies:{}", stopWatch.getTotalTimeMillis());
        return resultUrl;
//        return "login";
    }

    /**
     * 跳转到主页面
     */
    @GetMapping("/disk/home")
    public String home(Model model) {
        logger.info("跳转到主页面请求URL：{}", httpServletRequest.getRequestURL());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        logger.info("跳转到主页面数据处理开始");
        String resultUrl = pageProvider.homeHandle(model);
        logger.info("跳转到主页面数据处理结束,resultUrl:{}", resultUrl);
        stopWatch.stop();
        logger.info("跳转到主页面调用时间,millies:{}", stopWatch.getTotalTimeMillis());
        return resultUrl;
    }

    /**
     * 跳转到分享管理页面
     */
    @GetMapping("/share/manage")
    public String share(Model model) {
        logger.info("跳转到分享管理页面请求URL：{}", httpServletRequest.getRequestURL());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        logger.info("跳转到分享管理页面数据处理开始");
        String resultUrl = pageProvider.shareHandle(model);
        logger.info("跳转到分享管理页面数据处理结束,resultUrl:{}", resultUrl);
        stopWatch.stop();
        logger.info("跳转到分享管理页面调用时间,millies:{}", stopWatch.getTotalTimeMillis());
        return resultUrl;
    }

    /**
     * 查看分享页面
     */
    @GetMapping("/s/{shareId}")
    public String s(Model model, @PathVariable String shareId) {
        logger.info("查看分享页面请求URL：{}", httpServletRequest.getRequestURL());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        logger.info("查看分享页面数据处理开始");
        String resultUrl = pageProvider.sHandle(model, shareId);
        logger.info("查看分享页面数据处理结束,resultUrl:{}", resultUrl);
        stopWatch.stop();
        logger.info("查看分享页面调用时间,millies:{}", stopWatch.getTotalTimeMillis());
        return resultUrl;
    }

}
